package com.xiaoming.e3mall.service.impl;

import com.xiaoming.e3mall.common.pojo.EasyUITreeNode;
import com.xiaoming.e3mall.mapper.TbItemCatMapper;
import com.xiaoming.e3mall.pojo.TbItemCat;
import com.xiaoming.e3mall.pojo.TbItemCatExample;
import com.xiaoming.e3mall.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ItemCatServiceImpl implements ItemCatService{

    @Autowired
    private TbItemCatMapper itemCatMapper;

    @Override
    public List<EasyUITreeNode> getCategoryList(Long parentId) {
        // 1、根据parentId查询节点列表
        TbItemCatExample example = new TbItemCatExample();
        //设置查询条件
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        //执行查询
        List<TbItemCat> itemCats = itemCatMapper.selectByExample(example);

        //封装成页面需要的节点数据
        List<EasyUITreeNode> resultList = new ArrayList<>();

        for (TbItemCat itemCat : itemCats) {
            EasyUITreeNode node = new EasyUITreeNode();
            //设置商品分类的主键
            node.setId(itemCat.getId());
            //设置商品分类名称
            node.setText(itemCat.getName());
            //设置state：如果节点下有子节点“closed”，如果没有子节点“open”
            node.setState(itemCat.getIsParent()?"closed":"open");

            //添加到列表中
            resultList.add(node);
        }
        return resultList;
    }
}
